package com.example.newsenior;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.newsenior.databinding.FragmentDashboardBinding;
import com.example.newsenior.databinding.FragmentHomeBinding;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;


public class DashboardFragment extends Fragment {

        private FragmentDashboardBinding binding;

        public static DashboardFragment newInstance() {
            DashboardFragment fragment = new DashboardFragment();
            return fragment;
        }


        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            binding = FragmentDashboardBinding.inflate(inflater,container,false);
            return binding.getRoot();
        }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        //设置ViewPager的适配器Adapter
        DashboardPagerAdapter adapter = new DashboardPagerAdapter(this);
        binding.viewPager.setAdapter(adapter);
        binding.viewPager.setOffscreenPageLimit(adapter.getItemCount() - 1);
        //将TabLayout与ViewPager进行关联
        new TabLayoutMediator(binding.tabsDashboard, binding.viewPager, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText("dash %d" + position);
            }
        }).attach();
    }

    private static class DashboardPagerAdapter extends FragmentStateAdapter{

        public DashboardPagerAdapter(@NonNull Fragment fragment){
            super(fragment);
        }

        @NonNull
        @Override
        public Fragment createFragment(int position) {
            return DashboardChildFragment.newInstance(position);
        }

        @Override
        public int getItemCount() {
            return 2;
        }
    }
}